package com.yunxi.service.dubboService.impl.mes2pms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunxi.hubToBaseData.pojo.VehicleCertReqVO;
import com.yunxi.hubToBaseData.service.Mes2BaseDataDubboService;
import com.yunxi.model.vehicle.VehicleCert;
import com.yunxi.service.localService.vehicle.VehicleCertService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;

public class Mes2BaseDataDubboServiceImpl implements Mes2BaseDataDubboService {
    private static final Logger logger = LoggerFactory.getLogger(Mes2BaseDataDubboServiceImpl.class);

    @Autowired
    @Qualifier("vehicleCertServiceImpl")
    VehicleCertService vehicleCertService;

    @Override
    public void syncCert(List<VehicleCertReqVO> vehicleCertList) throws Exception {
        if(vehicleCertList!=null){
            for (int i=0;i<vehicleCertList.size();i++)
            {
                VehicleCertReqVO  o = vehicleCertList.get(i);
                QueryWrapper<VehicleCert> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("VIN",o.getVin());
                VehicleCert vc = vehicleCertService.getOne(queryWrapper);
                if(vc == null){
                    vc = new VehicleCert();
                }
                vc.setVin(o.getVin());
                vc.setCertNo(o.getCertNo());
                vc.setCertPrintDate(o.getCertPrintDate());
                vc.setVehicleCode(o.getVehicleCode());
                if(StringUtils.isNotEmpty(o.getVin())){
                    String relationId = vehicleCertService.getPatWayByVin(o.getVin());
                    String patWay = vehicleCertService.getPatWay(o.getVin());
                    if(StringUtils.isNotEmpty(patWay) && patWay.equals("A")){
                        vc.setCertStatus(1);//现金支付,合格证随车
                    }
                    else if(StringUtils.isEmpty(relationId)){
                        vc.setCertStatus(2);//财务审核
                    }
                    else if(StringUtils.isNotEmpty(relationId)){
                        vc.setCertStatus(3);//待发运
                        vc.setRelationId(relationId);
                    }
                }
                vehicleCertService.saveOrUpdateByVin(vc);
            }
        }
    }
}
